﻿// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) 2007 Microsoft Corporation. All rights reserved.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.Office.Tools;

namespace TShooter.TfsWord.AddIn.UIManager
{
    /// <summary>
    /// Collection which represents custom task panes specific for provided UIContainer.
    /// It filters data from the UIManager.CustomTaskPanes.
    /// </summary>
    /// <typeparam name="TWindow">Type of the Window.</typeparam>
    public sealed class WindowCustomTaskPaneCollection<TWindow> : IEnumerable<CustomTaskPane> 
        where TWindow : class
    {
        #region Fields

        private readonly CustomTaskPaneCollection allTaskPanes;
        private readonly UIContainerBase<TWindow> container;

        #endregion

        #region .ctor

        /// <summary>
        /// Creates a new instance of <see cref="WindowCustomTaskPaneCollection{TWindow}"/>.
        /// </summary>
        /// <param name="container">UIContainer holding the target window.</param>
        /// <param name="allTaskPanes">A collection of all custom task panes.</param>
        public WindowCustomTaskPaneCollection(UIContainerBase<TWindow> container, CustomTaskPaneCollection allTaskPanes)
        {
            this.container = container;
            this.allTaskPanes = allTaskPanes;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Find custom task panes by its child user control instance.
        /// </summary>
        /// <param name="control">UserControl to be found.</param>
        /// <returns>Returns the custom task panes containing the specified user control.</returns>
        public IEnumerable<CustomTaskPane> WithControl(UserControl control)
        {
            foreach (var taskPane in this)
            {
                if (taskPane.Control == control)
                {
                    yield return taskPane;
                }
            }
        }

        /// <summary>
        /// Find custom task panes by user control type.
        /// </summary>
        /// <typeparam name="T">Type of the user controls to be found.</typeparam>
        /// <returns>Returns the custom task panes with control of provided type.</returns>
        public IEnumerable<CustomTaskPane> WithControl<T>()
            where T : UserControl
        {
            foreach (var taskPane in this)
            {
                if (taskPane.Control is T)
                {
                    yield return taskPane;
                }
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Get a boolean value which indicates if there is any custom task pane specific for the container member.
        /// </summary>
        public bool IsEmpty
        {
            get
            {
                foreach (var taskPane in allTaskPanes)
                {
                    if (Equals(taskPane.Window, container.Window))
                    {
                        return false;
                    }
                }
                return true;
            }
        }

        #endregion

        #region IEnumerable<CustomTaskPane> Members

        public IEnumerator<CustomTaskPane> GetEnumerator()
        {
            foreach (var taskPane in allTaskPanes)
            {
                object window = null;

                try
                {
                    window = taskPane.Window;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }

                if (Equals(window, container.Window))
                {
                    yield return taskPane;
                }
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}